<?php

/**
 * This is the model class for table "Product".
 *
 * The followings are the available columns in table 'Product':
 * @property integer $id
 * @property string $name
 * @property string $details
 * @property integer $price
 * @property string $created
 * @property string $updated
 * @property integer $deleted
 *
 * Related records.
 * @property array $images
 */
class Product extends ActiveRecord
{
	/**
	 * @property CUploadedFile the uploaded image.
	 */
	public $image;

	/**
	 * Returns the static model of the specified AR class.
	 * @return Product the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{Product}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('name, price', 'required'),
			array('price', 'numerical'),
			array('name', 'length', 'max'=>255),
			array('image', 'file', 'types'=>'jpg, gif, png', 'allowEmpty'=>true),
			array('details, created, updated, deleted', 'safe'),
			array('id, name, price, created, updated', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		return array(
			'images' => array(self::HAS_MANY,'Image','parentId',
				'condition'=>'parent=:parent',
				'params'=>array(':parent'=>get_class($this))),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => Yii::t('label','Id'),
			'name' => Yii::t('label','Name'),
			'details' => Yii::t('label','Details'),
			'price' => Yii::t('label','Price'),
			'image' => Yii::t('label','Image'),
			'created' => Yii::t('label','Created'),
			'updated' => Yii::t('label','Updated'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('price',$this->price);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('updated',$this->updated,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Saves the image for this product.
	 * @return Image the saved image.
	 * @throws CException if the image could not be saved.
	 */
	public function saveImage()
	{
		if($this->images!==array())
			foreach($this->images as $image)
				$image->delete();

		if($this->image instanceof CUploadedFile)
			return Yii::app()->image->save($this->image,get_class($this),$this->id);
		else
			throw new CException('Could not save image. The uploaded image is not valid or empty!');
	}

	/**
	 * Renders the image for this product.
	 * @param string $version the image version name.
	 */
	public function renderImage($version)
	{
		if($this->images!==array())
		{
			$image=$this->images[0];
			$image->render($version);
		}
	}
}
